package com.health.system.security.auth.entity;

import com.health.system.entity.Role;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class HealthSecurityAuthDetails implements UserDetails {
    private Long id;

    private Long userInfoId;

    private String nickName;

    private String username;

    private String password;

    private Boolean isEnabled;

    private List<Role> roles;

    private LocalDateTime createTime;

    private LocalDateTime lastLoginTime;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> result = new ArrayList<>();

        getRoles().stream().map(Role::getRoleName).forEach(name -> result.add(new SimpleGrantedAuthority(name)));

        return result;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }

}
